/*
 * Alexey Danchuk
 * This software is an implementation of the task from OOP module.
 */
package factory;
import java.io.IOException;
import repository.QuestionsRepository;
import parser.FileParser;
import question.DiverseQuestion;
import question.LogicalQuestion;
import question.SimpleQuestion;

/**
 * This class describes database from file factory.
 *
 * @version 1.01 20 February 2013
 * @author Alexey Danchuk
 */
public class FileDatabase implements DatabaseFactory {
    
    /**
     * Questions repository.
     */
    QuestionsRepository questionsDatabase;
    private FileParser parser;

    @Override
    public void createDatabase(String fileName) throws IOException, Exception {
        QuestionsRepository.getInstance();
        String[][] data = getData(fileName);
        for (int i = 0; i < data.length; i++) {
            if ((data[i][2].split(" +")).equals("true")
                    || (data[i][2].split(" +")).equals("false")) {
                questionsDatabase.add(new LogicalQuestion(data[i][0],
                        data[i][1],data[i][2]));
            }
            if ((data[i][2].split(" +").length) == 1) {
                questionsDatabase.add(new SimpleQuestion(data[i][0],
                        data[i][1],data[i][2]));
            }
            if ((data[i][2].split(" +").length) > 1) {
                questionsDatabase.add(new DiverseQuestion(data[i][0],
                        data[i][1],data[i][2]));
            }
        }
    }
    
    private String[][] getData(String fileName) throws IOException {
        parser = new FileParser();
        parser.input.input(fileName);
        parser.parse(parser.input.getFileName());
        String[][] parsedData = parser.getFileRepresentation();
        return parsedData;
    }

    @Override
    public void createDatabase() throws IOException, Exception {
        throw new UnsupportedOperationException("You mast use"
                + " createDatabase(filename);"); 
    }
}
